package com.ae.shield.client.block.tileEntityRenderer;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.ae.shield.common.block.tile.ShieldWorkbenchTile;
import com.ae.shield.common.items.shieldFittings.makers.ShieldMakerBase;
import com.ae.shield.common.items.shieldFittings.SuffixItem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.model.ItemCameraTransforms;
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.item.ItemStack;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.Direction;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.items.IItemHandler;

/**
 * 用于给方块添加额外要渲染的东西
 * 此处用于在方块上渲染储存的物品
 */
public class ShieldWorkbenchBlockTER extends TileEntityRenderer<ShieldWorkbenchTile> {
    public ShieldWorkbenchBlockTER(TileEntityRendererDispatcher dispatcher) {
        super(dispatcher);
    }

    @Override
    public void render(ShieldWorkbenchTile tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
        IItemHandler inventory = tileEntityIn.getInventory();
        Direction direction = tileEntityIn.getBlockState().get(BlockStateProperties.HORIZONTAL_FACING);
        ItemRenderer itemRenderer = Minecraft.getInstance().getItemRenderer();
        byte facingRotation = 0;

        switch (direction) {
            case SOUTH:
                facingRotation = 2;
                break;
            case WEST:
                facingRotation = 3;
                break;
            case EAST:
                facingRotation = 1;
                break;
        }

        for (int i = 1; i < inventory.getSlots(); i++) {
            ItemStack stack = inventory.getStackInSlot(i);

            if (stack.getItem() instanceof SuffixItem) {
                float x = 0f;
                float z = 0f;
                float yR = 0f;
                matrixStackIn.push();

                switch (i) {
                    case 5:
                        x = 0.6f;
                        z = 0.5f;
                        yR = -90f;
                        break;
                    case 4:
                        x = 0.4f;
                        z = 0.8f;
                        yR = -120f;
                        break;
                    case 3:
                        x = 0.8f;
                        z = 0.2f;
                        yR = 30f;
                        break;
                    case 2:
                        x = 0.5f;
                        z = 0.3f;
                        yR = -200f;
                        break;
                    case 1:
                        x = 0.2f;
                        z = 0.7f;
                        yR = 90f;
                        break;
                }

                for (int j = 0; j < facingRotation; j++) {//TODO 提取旋转方法
                    float z1 = z;
                    z = x;
                    x = 1 - z1;
                }

                matrixStackIn.translate(x, 0.6d, z);
                matrixStackIn.scale(0.09f, 0.09f, 0.09f);
                matrixStackIn.rotate(new Quaternion(Vector3f.YN, 90f * facingRotation + yR, true));
                matrixStackIn.rotate(new Quaternion(Vector3f.XP, 90f, true));
                IBakedModel model = itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null);
                itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn, model);
                matrixStackIn.pop();
            }
        }

        ItemStack stack = inventory.getStackInSlot(0);
        if (stack.getItem() instanceof ShieldMakerBase) {
            matrixStackIn.push();
            float x = 0.713f;
            float z = 0.19f;

            for (int j = 0; j < facingRotation; j++) {//TODO 提取旋转方法
                float z1 = z;
                z = x;
                x = 1 - z1;
            }

            matrixStackIn.translate(x, 0.63d, z);
            matrixStackIn.scale(0.09f, 0.09f, 0.09f);
            matrixStackIn.rotate(new Quaternion(Vector3f.YN, 90f * facingRotation, true));
            IBakedModel model = itemRenderer.getItemModelWithOverrides(stack, tileEntityIn.getWorld(), null);
            itemRenderer.renderItem(stack, ItemCameraTransforms.TransformType.FIXED, true, matrixStackIn, bufferIn, combinedLightIn, combinedOverlayIn, model);
            matrixStackIn.pop();
        }
    }
}